﻿using System;
using System.Configuration;
using System.Text;
using System.Windows.Forms;
using Jayway.com.Neo4J.POC.Domain;
using Neo4jRestSharp;
using Neo4jRestSharp.Neo4jObjectServices;

namespace Jayway.com.Neo4J.POC
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            var appSettingsReader = new AppSettingsReader();
            var neo4jUriSetting = (string)appSettingsReader.GetValue("Neo4j uri", typeof(string));
            neo4jServerUri = new Uri(neo4jUriSetting);
        }

        Uri neo4jServerUri;

        Person neo, trinity, morpheus, cypher, agentSmith, theArchitect;

        private void btnLoad_Click(object sender, EventArgs e)
        {
			INodeObjectServices nodeObjectServices = ServicesFactory.ObjectServices(neo4jServerUri);

            //neo = new Person("Thomas Anderson");
            //nodeObjectServices.CreateNode(neo);

            //trinity = new Person("Trinity");
            //nodeObjectServices.CreateNode(trinity);

            //morpheus = new Person("Morpheus");
            //nodeObjectServices.CreateNode(morpheus);

            //cypher = new Person("Cypher");
            //nodeObjectServices.CreateNode(cypher);

            //agentSmith = new Program("Agent Smith");
            //nodeObjectServices.CreateNode(agentSmith);

            //theArchitect = new Program("The Architect");
            //nodeObjectServices.CreateNode(theArchitect);

            // Relationships
			IRelationshipObjectServices relServices = (IRelationshipObjectServices)nodeObjectServices;

            relServices.CreateRelationship(neo, RelationshipType.Knows, trinity);

            relServices.CreateRelationship(neo, RelationshipType.Knows, morpheus);

            relServices.CreateRelationship(morpheus, RelationshipType.Knows, trinity);

            relServices.CreateRelationship(morpheus, RelationshipType.Knows, cypher);

            relServices.CreateRelationship(cypher, RelationshipType.Knows, agentSmith);

            relServices.CreateRelationship(theArchitect, RelationshipType.Coded, agentSmith);
        }
        
        private void btnTraverse_Click(object sender, EventArgs e)
        {
            //INeo4jRelationshipRest neo4JRelRest =
            //    Neo4jStorageFactory.GetNodeStorageClient(neo4jServerUri);

            //var relationships =
            //    neo4JRelRest.GetRelationshipsOnNode(neo, RelationshipType.Knows);

            //var builder = new StringBuilder();
            //foreach (var friend in relationships)
            //{
            //    builder.Append(friend).Append(Environment.NewLine);
            //}

            //MessageBox.Show(builder.ToString(), "Knows each other in chain!", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            //INodeObjectServices neo4JNodeRest =
            //    Neo4jStorageFactory.GetNodeStorageClient(neo4jServerUri);

            //neo4JNodeRest.DeleteRelationships(neo);
            //neo4JNodeRest.DeleteRelationships(morpheus);
            //neo4JNodeRest.DeleteRelationships(cypher);
            //neo4JNodeRest.DeleteRelationships(theArchitect);

            //neo4JNodeRest.DeleteNode(neo);
            //neo4JNodeRest.DeleteNode(trinity);
            //neo4JNodeRest.DeleteNode(morpheus);
            //neo4JNodeRest.DeleteNode(cypher);
            //neo4JNodeRest.DeleteNode(agentSmith);
            //neo4JNodeRest.DeleteNode(theArchitect);

            //Close();
        }
    }
}